package com.pandy.transaction;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;

import javax.sql.DataSource;
import java.sql.Connection;


/**
 * @description:
 * @author: panhong
 * @date: 2025/3/19
 */
@Configuration
@ComponentScan(basePackages = "com.pandy") // 扫描指定包下的组件
@EnableTransactionManagement
//@EnableAspectJAutoProxy(exposeProxy = true)
public class AppConfig {

    @Bean
    public JdbcTemplate jdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }

    @Bean
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://192.168.5.129:3306/btest_engine");
        dataSource.setUser("root");
        dataSource.setPassword("PandySmile123!");
        return dataSource;
    }

    /**
     * 事务管理器
     * @return
     */
    @Bean
    public PlatformTransactionManager platformTransactionManager() {
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(dataSource());
        return manager;
    }
}
